package com.fla.shapeevader.ui;

import com.danikula.aibolit.Aibolit;
import com.danikula.aibolit.annotation.InjectView;
import com.fla.shapeevader.R;
import com.fla.shapeevader.model.BaseShape;
import com.fla.shapeevader.model.GameModel.GameMode;
import com.fla.shapeevader.model.support.ReflectionalTrajectory;
import com.fla.shapeevader.model.support.SinTrajectory;
import com.fla.shapeevader.ui.GameView.GameEventListener;
import com.fla.shapeevader.ui.drawers.BaseShapeDrawer;
import com.fla.shapeevader.ui.drawers.RectDrawer;
import com.fla.shapeevader.ui.drawers.UserShapeDrawer;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String LOG_TAG = MainActivity.class.getName();
    
    @InjectView(R.id.mainDrawer)
    private GameView mainDrawer;
    
    @InjectView(R.id.currentTime)
    private Chronometer currentTimer;
    
    @InjectView(R.id.bestTime)
    private Chronometer bestTimer;
    
    private GameMode currentMode = GameMode.MAIN;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        Aibolit.setInjectedContentView(this, R.layout.main);
        
        initGameView();
    }
    
    private void startCurrentTimer() {
        currentTimer.setBase(SystemClock.elapsedRealtime());
        currentTimer.start();
    }
    
    private void stopCurrentTimer() {
        currentTimer.stop();
    }

    private float getRelative() {
        return getResources().getDisplayMetrics().widthPixels / 10;
    }
    
    private void initGameView() {
        BaseShape userShape = new BaseShape(new PointF(getRelative() * 5, getRelative() * 8), getRelative() * 2, getRelative() * 2, null);
        UserShapeDrawer userShapeDrawer = new UserShapeDrawer(userShape);
        
        BaseShapeDrawer[] currentShapes = null;
        switch (currentMode) {
            case MAIN:
                currentShapes = getShapesMainMode();
                break;
            case RUSH:
                currentShapes = getShapesRushMode();
                break;
            default:
                currentShapes = getShapesMainMode();
        }
        
        mainDrawer.setGameEventListener(new MyGameEventListener());

        mainDrawer.initAndStart(userShapeDrawer, currentShapes);
        startCurrentTimer();
    }
    
    private BaseShapeDrawer[] getShapesMainMode() {
        float relative = getRelative();
        float speed = relative / 6;
        
        BaseShape shape = new BaseShape(new PointF(0, 0), relative, relative, new ReflectionalTrajectory(0.1f, relative / speed));
        BaseShapeDrawer shapeDrawer = new RectDrawer(shape, Color.BLUE);

        BaseShape shape2 = new BaseShape(new PointF(150, 0), 2.5f * relative,  2.5f * relative, new ReflectionalTrajectory(0.55f, speed));
        BaseShapeDrawer shapeDrawer2 = new RectDrawer(shape2, Color.RED);

        BaseShape shape3 = new BaseShape(new PointF(100, 0),  2 * relative, 2 * relative, new ReflectionalTrajectory(1f, speed));
        BaseShapeDrawer shapeDrawer3 = new RectDrawer(shape3, Color.GRAY);
        
        return new BaseShapeDrawer[]{shapeDrawer, shapeDrawer2, shapeDrawer3};
    }
    
    private BaseShapeDrawer[] getShapesRushMode() {
        float relative = getRelative();
        float speed = relative / 6;
        
        BaseShape shape = new BaseShape(new PointF(0, -100), relative, relative, new SinTrajectory(0.1f, relative / speed));
        BaseShapeDrawer shapeDrawer = new RectDrawer(shape, Color.BLUE);

        BaseShape shape2 = new BaseShape(new PointF(200, 0), 2.5f * relative,  2.5f * relative, new SinTrajectory(0.55f, speed));
        BaseShapeDrawer shapeDrawer2 = new RectDrawer(shape2, Color.RED);

        BaseShape shape3 = new BaseShape(new PointF(100, -200),  2 * relative, 2 * relative, new SinTrajectory(1f, speed));
        BaseShapeDrawer shapeDrawer3 = new RectDrawer(shape3, Color.GRAY);
        
        return new BaseShapeDrawer[]{shapeDrawer, shapeDrawer2, shapeDrawer3};
    }
    
    private void showChooseModeDialog() {
        // onDismiss - resume game
        final Dialog d = new Dialog(this);
        d.setTitle("Choose game mode");
        d.setContentView(R.layout.choose_mode_dialog);

        ((Button) d.findViewById(R.id.mainMode)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                currentMode = GameMode.MAIN;
                initGameView();
                d.dismiss();
            }
        });
        
        ((Button) d.findViewById(R.id.rushMode)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                currentMode = GameMode.RUSH;
                initGameView();
                d.dismiss();
            }
        });
        
        d.show();
    }
    
    private void showHelpDialog() {
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        
        MenuItem restart = menu.findItem(R.id.restart);
        restart.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                initGameView();
                return true;
            }
        });
        
        MenuItem chooseMode = menu.findItem(R.id.chooseMode);
        chooseMode.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                // pause game
                showChooseModeDialog();
                return true;
            }
        });
        
        MenuItem help = menu.findItem(R.id.help);
        help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                showHelpDialog();
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
    
    private class MyGameEventListener implements GameEventListener {
        @Override
        public void onIntersectionOccured() {
            stopCurrentTimer();
            Toast.makeText(MainActivity.this, "intersection occured", Toast.LENGTH_SHORT).show();
        }
    }
    
    private class GameTimer extends CountDownTimer {
        public GameTimer(long millisInFuture, long countDownInterval) {
            super(Long.MAX_VALUE, 100);
        }

        @Override
        public void onFinish() {
            
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // increase speed here
        }
    }
}